import {createRouter, createWebHashHistory} from 'vue-router'

const router=createRouter({
        history: createWebHashHistory(),
        routes: [
            {
                path:'/',
                component:()=>import('./components/Login.vue')
            },
            {
                path:'/login',
                component:()=>import('./components/Login.vue')
            },
            {
                path:'/register',
                component:()=>import('./components/Register.vue')
            },
            {
                path:'/reset',
                component:()=>import('./components/Reset.vue')
            },
            {
              path:'/403'
              ,component:()=>import('./components/Auth.vue')
            },
            {
                path:'/admin',
                component:()=>import('./components/admin/Main.vue'),
                children:[
                    {
                        path:'/admin/class'
                        ,component:()=>import('./components/admin/class/class.vue')
                    },
                    {
                        path:'/admin/student'
                        ,component:()=>import('./components/admin/student/student.vue')
                    },
                    {
                        path:'/admin/teacher'
                        ,component:()=>import('./components/admin/teacher/teacher.vue')
                    },
                    {
                        path:'/admin/inform'
                        ,component:()=>import('./components/admin/inform/inform.vue')
                    },
                    {
                        path:'/admin/review'
                        ,component:()=>import('./components/admin/teacher/review.vue')
                    },
                    {
                        path:'/admin/course'
                        ,component:()=>import('./components/admin/course/main.vue'),
                        children:[
                            {
                                path:'/admin/course/info'
                                ,component:()=>import('./components/admin/course/course.vue')
                            },
                            {
                                path:'/admin/course/timetable'
                                ,component:()=>import('./components/admin/course/timetable.vue')
                            }
                        ]
                    },
                    {
                        path:'rateCheck'
                        ,component:()=>import('./components/admin/rate/rateCheck.vue')
                    },
                    {
                        path:'issueRate'
                        ,component:()=>import('./components/admin/rate/issueRate.vue')
                    },
                ]
            },
            {
                path:'/teacher',
                component:()=>import('./components/teacher/Main.vue'),
                children:[
                    {
                        path: '/teacher/home'
                        ,component:()=>import('./components/teacher/home/home.vue')
                    },
                    {
                      path: '/teacher/timetable'
                        ,component:()=>import('./components/teacher/timetable/timetable.vue')
                    },
                    {
                        path: '/teacher/publish/course'
                        ,component:()=>import('./components/teacher/publish/course.vue')
                    },
                    {
                        path: '/teacher/class/attend'
                        ,component:()=>import('./components/teacher/class/attend.vue')
                    },
                    {
                        path: '/teacher/class/score'
                        ,component:()=>import('./components/teacher/class/score.vue')
                    },
                    {
                        path: '/teacher/class/student',
                        component:()=>import('./components/teacher/class/student.vue')
                    },
                    {
                        path: '/teacher/inform/notice',
                        component:()=>import('./components/teacher/inform/notice.vue')
                    },
                    {
                      path: '/teacher/inform/attend',
                      component:()=>import('./components/teacher/inform/attend.vue')
                    },
                    {
                        path: '/teacher/student/attend',
                        component:()=>import('./components/teacher/student/attend.vue')
                    },
                    {
                        path: '/teacher/student/score',
                        component:()=>import('./components/teacher/student/score.vue')
                    },
                    {
                        path: '/teacher/student/rate',
                        component:()=>import('./components/teacher/student/rate.vue')
                    },
                ]
            },
            {
                path:'/student',
                component:()=>import('./components/student/Main.vue'),
                children:[
                    {
                        path: '/student/home'
                        ,component:()=>import('./components/student/home/home.vue')
                    },
                    {
                        path: '/student/course/mycourse'
                        ,component:()=>import('./components/student/course/mycourse.vue')
                    },
                    {
                        path: '/student/course/timetable',
                        component:()=>import('./components/student/course/timetable.vue')
                    },
                    {
                        path: '/student/course/myscore'
                        ,component:()=>import('./components/student/course/myscore.vue')
                    },
                    {
                        path: '/student/course/myattend',
                        component:()=>import('./components/student/course/myattend.vue')
                    },
                    {
                        path: '/student/course/secourse',
                        component:()=>import('./components/student/course/secourse.vue')
                    },
                    {
                        path: '/student/course/drop',
                        component:()=>import('./components/student/course/drop.vue')
                    },
                    {
                        path: '/student/course/secourse_record',
                        component:()=>import('./components/student/course/secourse record.vue')
                    },
                    {
                        path: '/student/inbox',
                        component:()=>import('./components/student/inbox/inbox.vue')
                    },
                    {
                        path:'/student/rate'
                        ,component:()=>import('./components/student/rate/startRate.vue')
                    }
                ]
            },
        ]
    }
);
router.beforeEach((to,from,next)=>{
    //to 表示要跳转到的路径
    //from 表示从哪个路径跳转来的
    //next 是一个函数 表示放行
    let adminPaths=["/admin"];
    let teacherPaths=["/teacher"];
    let studentPaths=["/student"];
    let user=JSON.parse(localStorage.getItem("user") || "{}");
    if(user.data!=='1' && adminPaths.includes(to.path)){
        next("/403")
    } else if(user.data!=='2' && teacherPaths.includes(to.path)){
        next("/403")
    } else if(user.data!=='3' && studentPaths.includes(to.path)){
        next("/403")
    } else{
        next()
    }
})
export default router;